package com.admin.education.dataTransferObject.userInformation.Mapper;

import com.admin.education.dataTransferObject.classes.mapper.ClassesMapper;
import com.admin.education.dataTransferObject.role.mapper.RoleMapper;
import com.admin.education.dataTransferObject.schools.mapper.SchoolsMapper;
import com.admin.education.dataTransferObject.userInformation.UserInformationTransfer;
import com.admin.education.models.Schools;
import com.admin.education.models.UserInformation;
import jdk.internal.dynalink.support.ClassMap;
import net.bytebuddy.asm.Advice;
import org.joda.time.*;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Mapper
public abstract class UserInformationMapper {
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private SchoolsMapper schoolsMapper;
    @Mappings({
            @Mapping(target = "classes", ignore = true),
            @Mapping(target = "schools", ignore = true),
            @Mapping(target = "roles", ignore = true),
            @Mapping(target = "lastLoginTime", ignore = true),
            @Mapping(target = "fanCount", ignore = true),
            @Mapping(target = "attentionCount", ignore = true)
    })
    public abstract UserInformationTransfer mapFrom(UserInformation userInformation);

    @Mappings({})
    public abstract List<UserInformationTransfer> mapFrom(List<UserInformation> userInformations);

    @AfterMapping
    public  void afterMapping(UserInformation userInformation, @MappingTarget UserInformationTransfer userInformationTransfer) {
        if (null != userInformation.getClasses()) {
            userInformationTransfer.setClasses(classesMapper.mapFrom(userInformation.getClasses()));
        }

        if (null != userInformation.getRoles()) {
            userInformationTransfer.setRoles(roleMapper.mapFrom(userInformation.getRoles()));
        }

        if (null != userInformation.getSchools()) {
            Schools schools = userInformation.getSchools();
            schools.setClasses(null);
            userInformationTransfer.setSchools(schoolsMapper.mapFrom(schools));
        }
        Date now = new Date();
        if (null != userInformation.getLastLoginTime()) {
            DateTime dt1 = new DateTime(userInformation.getLastLoginTime());
            DateTime dt2 = new DateTime(now);
            Days days = Days.daysBetween(dt1, dt2);
            Hours hours = Hours.hoursBetween(dt1, dt2);
            Minutes minutes = Minutes.minutesBetween(dt1, dt2);
            Seconds seconds = Seconds.secondsBetween(dt1, dt2);
            if (days.getDays() != 0) {
                userInformationTransfer.setLastLoginTime(Days.daysBetween(dt1, dt2).getDays() + "天前");
            } else if (hours.getHours() % 24 != 0) {
                userInformationTransfer.setLastLoginTime(hours.getHours() + "小时前");
            } else if (minutes.getMinutes() % 60 != 0) {
                userInformationTransfer.setLastLoginTime(minutes.getMinutes() % 60 + "分钟前");
            } else if (seconds.getSeconds() % 60 != 0) {
                userInformationTransfer.setLastLoginTime(seconds.getSeconds() % 60 + "秒前");
            }
        }

        if (null != userInformation.getFans()) {
            userInformationTransfer.setFanCount(userInformation.getFans().size());
        }

        if (null != userInformation.getAttentions()) {
            userInformationTransfer.setAttentionCount(userInformation.getAttentions().size());
        }
    }

}
